package health.lm.com.order.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.component_base.widget.SuperDividerItemDecoration;
import health.lm.com.order.R;
import health.lm.com.order.R2;
import health.lm.com.order.adapter.OrderListAdapter;
import health.lm.com.order.entity.OrderListEntity;
import health.lm.com.order.presenter.OrderListPresent;
import health.lm.com.router.ARouterConstant;

import static com.lm.sjy.titlebar.widget.CommonTitleBar.ACTION_LEFT_BUTTON;

/**
 * 订单列表
 *
 * @author GQI
 */
@Route(path = ARouterConstant.OrderListActivity)
public class OrderListActivity extends XListActivity<OrderListPresent> {
    @BindView(R2.id.title_bar)
    CommonTitleBar titlebar;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R2.id.rlv_jf_detail)
    RecyclerView rlvJfDetail;
    private List<OrderListEntity.Order_list> beanList;
    private OrderListAdapter adapter;
    private boolean isRefresh;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_list;
    }

    @Override
    public OrderListPresent newP() {
        return new OrderListPresent();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initAdapter();
        super.rlRefreshLayout = srlLayout;
        super.adapter = adapter;
        super.recyclerView = rlvJfDetail;
        super.initData(savedInstanceState);
        titlebar.setListener((v, action, extra) -> {
            if (action == ACTION_LEFT_BUTTON) {
                finish();
            }
        });
    }

    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        this.isRefresh = isRefresh;
        getP().getListMore(isRefresh, layout, page, pageSize);
    }


    /**
     * 初始化适配器
     */
    private void initAdapter() {
        beanList = new ArrayList<>();
        adapter = new OrderListAdapter(beanList);
        rlvJfDetail.setLayoutManager(new LinearLayoutManager(mActivity));
        rlvJfDetail.addItemDecoration(new SuperDividerItemDecoration(new SuperDividerItemDecoration.Builder(context)
                .setDividerColor(0xFFeeeeee)
                .setDividerWidth(12)
                .setOrientation(RecyclerView.VERTICAL)
                .setIsShowLastDivide(true)));
        rlvJfDetail.setAdapter(adapter);
    }

    public void getListSuccess(List<OrderListEntity.Order_list> beanlist) {
        this.beanList = beanlist;
        if(beanList == null) {
            return;
        }
        if (isRefresh && beanList.size() >= pageSize) {
            initLoadMore();
        }
        notifyData();
    }


    private void notifyData() {
        if (beanList.size() < pageSize) {
            adapter.loadMoreEnd();
        } else {
            adapter.loadMoreComplete();
        }
        if (isRefresh) {
            adapter.setNewData(beanList);
        } else {
            adapter.addData(beanList);
        }
        if (beanList.size()<=0){
            adapter.setEmptyView(empty());
        }
    }
}
